#include <QCoreApplication>
#include <QtSql>
#include <QDebug>
#include <QVector>
int main(int argc, char *argv[])
{
    QString connectionName("playlist_connection");

    QCoreApplication a(argc, argv);
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
        db.setDatabaseName("media.db");

        if(!db.open()) {
            qDebug() << db.lastError();
            return -1;
        }
        QSqlQuery query(db);
        query.exec("CREATE TABLE AudioFiles ("
                   "path VARCHAR(255) NOT NULL,"
                   "modify DATETIME,"
                   "title VARCHAR(255),"
                   "artist VARCHAR(255),"
                   "album VARCHAR(255),"
                   "genre VARCHAR(255),"
                   "PRIMARY KEY (path)"
                   ");");

#if 0
        const QVector<QString> v({":path", ":modify", ":title", ":artist", ":album", ":genre"});
        query.prepare(QString("INSERT INTO AudioFiles VALUES(%1, %2, %3, %4, %5, %6);").
                      arg(v[0]). arg(v[1]). arg(v[2]). arg(v[3]). arg(v[4]). arg(v[5]));
        query.bindValue(v[0], "C:/Users/semisky/music/1.mp3");
        query.bindValue(v[1], "2000-11-07 12:00:00");
        query.bindValue(v[2], "Pretty Girl");
        query.bindValue(v[3], "jay chou");
        query.bindValue(v[4], "jay");
        query.bindValue(v[5], "unknows");
        if(!query.exec()) {
            qDebug() << query.lastError();
        }
#else
        //Insert Row
        //if one error, other querys not perform.
        query.prepare("INSERT INTO AudioFiles VALUES(?, ?, ?, ?, ?, ?);");
        QVariantList path, modify, title, artist, album, genre;

        path << "C:/Users/semisky/music/1.mp3" << "C:/Users/semisky/music/2.mp3";
        query.addBindValue(path);

        modify << "2000-11-07 12:00:00" << "2000-11-07 12:00:01";
        query.addBindValue(modify);

        title << "Pretty Girl" << "Pretty Boy";
        query.addBindValue(title);

        artist << "jay chou" << "jay chou";
        query.addBindValue(artist);

        album << "jay" << "jay";
        query.addBindValue(album);

        genre << QVariant(QVariant::String) << "popluar";
        query.addBindValue(genre);

        if(!query.execBatch()) {
            qDebug() << query.lastError();
        }
#endif
        //Select Row
        query.exec("SELECT * FROM AudioFiles WHERE path LIKE '%mp3'");
        while(query.next()) {
#if 0
            qDebug() << query.record();
#else
            qDebug() << query.value(0).toString() <<
                        query.value("modify").toString() <<
                        query.value(2).toString() <<
                        query.value(3).toString() <<
                        query.value(4).toString() <<
                        query.value(5).toString();
#endif
        }
        db.close();
    }
    QSqlDatabase::removeDatabase(connectionName);

    return a.exec();
}

